package com.orange.common.base.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.orange.common.base.httpConfig.HttpAPIService;
import com.orange.common.base.mail.MailService;
import com.orange.common.base.redis.utils.RedisCache;
import com.orange.common.base.service.BaseService;
import com.orange.common.utils.IpUtil;
import com.orange.common.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Service基类
 *
 * @Author YWF
 * @Date 2022/1/16 21:26
 */
public class BaseServiceImpl<M extends BaseMapper<T>,T> extends ServiceImpl<M,T> implements BaseService<T> {

    protected final static Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);

    @Value("${app.developer.cache_key}")
    protected String appInfoCacheKey;

    @Autowired
    protected MailService mailService;

    @Autowired(required = false)
    private HttpServletRequest request;

    @Autowired(required = false)
    private HttpServletResponse response;

    @Autowired
    protected RedisCache redisCache;

    @Autowired
    protected HttpAPIService httpAPIService;

    @Override
    public HttpServletRequest getRequest() {
        return this.request;
    }

    @Override
    public HttpServletResponse getResponse() {
        return this.response;
    }

    @Override
    public String getIp() {
        String localIP = IpUtil.getIpAddr(request);
        logger.info("局域网IP地址查询：{}", localIP);
        return localIP;
    }

    @Override
    public String getWebRoot(){
        return request.getServletContext().getRealPath("/").replace("\\", "/");
    }

    @Override
    public String getTerminal(){
        String terminal = request.getHeader("User-Agent");
        if(terminal.contains("Windows NT")){
            terminal = "PC";
        }else{
            terminal = "MOBILE";
        }
        return terminal;
    }

    @Override
    public PageData getPageData() {
        return new PageData(this.getRequest());
    }

}